package com.scaffold.commons.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import wangjg.commons.io.FileIOHelper;

/**
 * 文件服务
 * @author wangjg
 *
 */
public class LocalFileSystem implements FileIO{
	private String rootDir;
	
	/**
	 * @param rootDir
	 */
	public LocalFileSystem(String rootDir) {
		this.rootDir = rootDir;
	}
	
	public LocalFileSystem() {
	}

	@Override
	public void save(String path, InputStream in) throws IOException {
		File file = this.getFile(path, true);
		FileIOHelper.StreamToFile(in, file);
	}

	@Override
	public void load(String path, OutputStream out) throws IOException {
		File file = this.getFile(path);
		FileIOHelper.FileToStream(file, out);
	}

	@Override
	public boolean exists(String path) throws IOException {
		File file = this.getFile(path);
		return file.exists();
	}

	@Override
	public boolean delete(String path) throws IOException {
		File file = this.getFile(path);
		return file.delete();
	}

	@Override
	public boolean move(String srcPath, String destPath) throws IOException {
		File file = this.getFile(srcPath);
		File destFile = this.getFile(destPath, true);
		boolean success = file.renameTo(destFile);
		return success;
	}
	
	public String getRootDir() {
		return rootDir;
	}

	public void setRootDir(String rootDir) {
		this.rootDir = rootDir;
	}

	@Override
	public long getLength(String path) throws IOException {
		File file = this.getFile(path);
		return file.length();
	}

	@Override
	public InputStream getInputStream(String path) throws IOException {
		File file = this.getFile(path);
		return new FileInputStream(file);
	}

	@Override
	public OutputStream getOutputStream(String path) throws IOException {
		File file = this.getFile(path);
		File pf = file.getParentFile();
		pf.mkdirs();
		return new FileOutputStream(file);
	}

	@Override
	public List<String> getChildren(String path) throws IOException {
		List<String> list = new ArrayList<String>();
		File file = this.getFile(path);
		String[] flist = file.list();
		if(flist!=null){
			for(String f:flist){
				list.add(f);
			}	
		}
		return list;
	}
	
	private File getFile(String path){
		return this.getFile(path, false);
	}
	private File getFile(String path, boolean mkdirs){
		if(path!=null &&path.length()>255){
			throw new RuntimeException("path is too long.");
		}
		File file = new File(rootDir, path);
		if(path!=null && path.indexOf('/')!=-1){
			if(mkdirs){
				file.getParentFile().mkdirs();
			}
		}
		return file;
	}

	@Override
	public FileDetail getFileStatus(String path) throws IOException {
		File file = new File(rootDir, path);
		FileDetail fd = new FileDetail();
		fd.setSize(file.length());
		fd.setDir(file.isDirectory());
		return fd;
	}

}